/**
 * 
 */
package org.pump.client.util;

import java.util.Properties;

import org.apache.mina.core.future.CloseFuture;
import org.apache.mina.core.future.IoFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liudong03
 *
 */
public class PumpUtil {
	private static final Logger mylog = LoggerFactory.getLogger(PumpUtil.class);
	public static String readPropertyString(Properties conf,String key)
	{
		return conf.getProperty(key);
		
	}
	
	public static int readPropertyInt(Properties conf,String key)
	{
		String value = conf.getProperty(key);
		if(value == null)
		{
			mylog.error("the property " + key + "is not exists");
			return -1;
		}
		
		int intvalue = Integer.parseInt(value);
		return intvalue;
	}
	
	public static boolean activeCloseSession(IoSession session)
	{
		session.setAttribute(MyConstant.CLOSE_TYPE, MyConstant.CLIENT_ACTIVE_CLOSE);
		CloseFuture cf = session.getCloseFuture();
		IoFutureListener<IoFuture> closeListener = new IoFutureListener<IoFuture>() {
		        public void operationComplete(IoFuture future) {
		            mylog.info(future.getSession() + " is close");
		        }};
		cf.addListener(closeListener);
		cf.getSession().close(false);
		cf.awaitUninterruptibly();
		
		return true;
	}
}
